package com.smartview.jboss.spring.mvc;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.smartview.jboss.spring.dto.WeatherDTO;
import com.smartview.jboss.spring.dao.WeatherDao;
import com.smartview.jboss.spring.model.DataFormatter;
import com.smartview.jboss.spring.model.DataParser;
import com.smartview.jboss.spring.model.ShoppingCart;
import com.smartview.jboss.spring.model.YahooData;

@Controller
public class WeatherService {
	@Autowired
	private YahooData data;
	
	@Autowired
	private DataParser parser;
	
	@Autowired
	private DataFormatter formatter;
	
	@Autowired
	private WeatherDao wd;
	
	@Autowired
	private ShoppingCart cart;

	@RequestMapping("/getWeather")
	public ModelAndView init2() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("getWeather");
		
		// Using the DAO to get the weather info list from DB
		List<WeatherDTO> list = wd.getWeatherList();
		
		// Add this list to this dynamic web page "getWeather.jsp". Name this list "weatherList"
		mav.addObject("weatherList", list);
		return mav;
	}
/*	@RequestMapping(method = RequestMethod.GET)
	public @ModelAttribute("message") String readWeather() {
		return "please enter a zip code";
	}*/
	

	@RequestMapping(value = "/showWeather", method = RequestMethod.POST)
	public @ModelAttribute("weatherInfo") String readWeatherByZip(@RequestParam("zip") String zip) {
		// First we check if the db has the weather inforamtion
		// If yes, we get the data and show it
		// If no, we need to get the data from Yahoo Weather service
		
		// You need to get the live weather and return the real one not the fake one
		try {
			WeatherDTO w = wd.getWeather(zip);
			if (w == null) {
				w = parser.parseWeather(zip, data.retrieveWeather(zip));
				wd.saveWeather(w);
			} 
			
			return formatter.formatWeather(w);
		} catch (ParseErrorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MethodInvocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ResourceNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "no data";
	}
	
	@RequestMapping(value = "/shopping")
	public ModelAndView doShopping() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("shopping");
		return mav;
		
	}
	
	@RequestMapping(value = "/shoppingCart")
	// How can we get the user choices?
	public ModelAndView showShoppingCart(HttpServletRequest request) {
		// Get the user's choice
		String toBuy = request.getParameter("item");
		cart.add(toBuy);
		
		// We will put the "toBuy" into a shopping cart, we will do this a little bit later
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName("shoppingCart");
		
		// Get everything from the shopping cart
		List<String> list = cart.getItems();
		mav.addObject("data", list);
		return mav;
	}
	
	@RequestMapping(value = "/checkout")
	public void payAndLeave(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// I want to destroy the session here
		request.getSession().invalidate();
		
		// I want to go back to the main page
		response.sendRedirect("/smartView/spring/");
	}
}

















